﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.SiteMap;
using Telerik.Web.UI;

public partial class SiteWorx_NavigationManager : System.Web.UI.Page
{
    public LinkedList<PageInfo> MyPages
    {
        get
        {
            if (Session["MyPages"] == null)
                Session["MyPages"] = Asah.SiteWorx.SiteMap.NavigationMenu.GetFullMenu();
            return (LinkedList<PageInfo>)Session["MyPages"];
        }
        set
        {
            Session["MyPages"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            MyPages = null;
            LoadNavigationMenu();
        }
    }

    private void LoadNavigationMenu()
    {
        rtvNavigationMenu.Nodes.Clear();
        RadTreeNode radTreeNode;
        foreach (PageInfo pageInfo in MyPages)
        {
            radTreeNode = new RadTreeNode();
            radTreeNode.Text = pageInfo.MenuText;
            radTreeNode.Value = pageInfo.ID.ToString();
            if (pageInfo.FileName.ToUpper() == "DEFAULT.ASPX")
                radTreeNode.AllowDrag = false;
            LoadSubNavigationMenu(pageInfo.ChildPages, ref radTreeNode);
            rtvNavigationMenu.Nodes.Add(radTreeNode);
        }
    }

    private void LoadSubNavigationMenu(LinkedList<PageInfo> childPages, ref RadTreeNode radTreeNode)
    {
        RadTreeNode radSubTreeNode;
        foreach (PageInfo pageInfo in childPages)
        {
            radSubTreeNode = new RadTreeNode();
            radSubTreeNode.Text = pageInfo.MenuText;
            radSubTreeNode.Value = pageInfo.ID.ToString();
            LoadSubNavigationMenu(pageInfo.ChildPages, ref radSubTreeNode);
            radTreeNode.Nodes.Add(radSubTreeNode);
        }
    }

    protected void rtvNavigationMenu_NodeDrop(object sender, RadTreeNodeDragDropEventArgs e)
    {
        try  // try to move the row however if it fails we just ignore it.
        {
            PageInfo sourcePageInfo = null;
            int sourcePageId = int.Parse(e.SourceDragNode.Value);
            int destinationPageId = int.Parse(e.DestDragNode.Value);

            // First we need to find the source page object in the list and remove it.
            FindAndRemoveSourcePage(sourcePageId, ref sourcePageInfo, MyPages);

            // Next we need to find the destination page object in the list and insert the source.
            FindAndInsertSourcePage(destinationPageId, ref sourcePageInfo, MyPages, e.DropPosition);

            // Reload the navigation menu tree.
            LoadNavigationMenu();
        }
        catch
        {
        }
    }

    private void FindAndRemoveSourcePage(int sourcePageId, ref PageInfo sourcePageInfo, LinkedList<PageInfo> pages)
    {
        foreach (PageInfo info in pages)
        {
            if (info.ID == sourcePageId)
            {
                sourcePageInfo = info;
                pages.Remove(info);
                return;
            }
            else
                FindAndRemoveSourcePage(sourcePageId, ref sourcePageInfo, info.ChildPages);
        }
    }

    private void FindAndInsertSourcePage(int destinationPageId, ref PageInfo sourcePageInfo, LinkedList<PageInfo> pages, 
                                         RadTreeViewDropPosition dropPosition)
    {
        LinkedListNode<PageInfo> currentNode = pages.First;
        bool keepLooping = true;

        while (currentNode != null && keepLooping)
        {
            if (currentNode.Value.ID == destinationPageId) // found the destination page.
            {
                switch (dropPosition)
                {
                    case RadTreeViewDropPosition.Over:
                        currentNode.Value.ChildPages.AddLast(sourcePageInfo);
                        keepLooping = false;
                        break;
                    case RadTreeViewDropPosition.Above:
                        pages.AddBefore(currentNode, sourcePageInfo);
                        keepLooping = false;
                        break;
                    case RadTreeViewDropPosition.Below:
                        pages.AddAfter(currentNode, sourcePageInfo);
                        keepLooping = false;
                        break;
                        
                }
            }
            else
                FindAndInsertSourcePage(destinationPageId, ref sourcePageInfo, currentNode.Value.ChildPages, dropPosition);
            currentNode = currentNode.Next;
        }
    }

    protected void  rtvNavigationMenu_PreRender(object sender, EventArgs e)
    {
        rtvNavigationMenu.ExpandAllNodes();
    }

    protected void  btnSave_Click(object sender, EventArgs e)
    {
        Asah.SiteWorx.SiteMap.NavigationMenu.UpdateMenu(MyPages);
    }
       
}